#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

int minPathSum(vector<vector<int>>& grid)
{
    vector<vector<int>>dp(grid);
    for (int i = 0; i < dp.size(); i++)
    {
        for (int j = 0; j < dp[0].size(); j++)
        {
            if(j==0&&i==0)
            {
                dp[i][j]=grid[i][j];
            }else if(i==0)
            {
                dp[i][j]=dp[i][j-1]+grid[i][j];
            }else if(j==0)
            {
                dp[i][j]=dp[i-1][j]+grid[i][j];
            }else
            {
                dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j];
            }
        }
        
    }
    return dp[dp.size()-1][dp[0].size()-1];
}

int main()
{
    vector<vector<int>> gird{{1,3,1},{1,5,1},{4,2,1}};
    cout << minPathSum(gird)<<endl;
    return 0;
}